package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.SsoService;
import com.yswy.backstage.service.UserProjectService;
import com.yswy.common.utils.ListUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.UserProject;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.UserProjectMapper;
import com.yswy.domain.vo.dto.BaseFilter;
import com.yswy.domain.vo.gld.addOrUpate.AppProjectAddOrUpdate;
import com.yswy.backstage.service.AppProjectService;
import com.yswy.domain.entity.AppProject;
import com.yswy.domain.mapper.AppProjectMapper;
import com.yswy.domain.vo.gld.search.PackageManageSearch;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.list.AbstractLinkedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.createOrUpdate;
import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;

@Slf4j
@Service
public class AppProjectServiceImpl  extends ServiceImpl<AppProjectMapper, AppProject>
        implements AppProjectService {

    @Autowired
    private UserProjectService userProjectService ;
    @Autowired
    private SsoService ssoService;
    @Override
    public Page<AppProject> ListAppProject(BaseFilter req) {
        Page<AppProject> payOrderPage = baseMapper.pageAppProject(req
                , new Page<>(req.getPageNum() , req.getPageSize()) );
        return payOrderPage;
    }

    @Override
    public void addAppProject(AppProjectAddOrUpdate appProjectReqVO) {
        User user = CurrentUserContext.getCurrentUser();
        AppProject appProject = null ;
        if (appProjectReqVO.getId() != null){
            appProject = getById(appProjectReqVO.getId());
            Assert.state(null != appProject && null != appProject.getId(), NULL_MSG);
            BeanUtil.copyProperties(appProjectReqVO, appProject);
            BasePojo.setBase(user.getId().longValue(), false, appProject);
            updateById(appProject);
        }else {
            appProject = new AppProject(appProjectReqVO , user);
            save(appProject);
        }

        if (ListUtils.isNotNull(appProjectReqVO.getUserIds())){
            List<UserProject> userProjects = new ArrayList<>();
            for (Long id:appProjectReqVO.getUserIds() ) {
                userProjects.add(new UserProject(id ,appProject.getId()));

            }
            userProjectService.remove(new LambdaQueryWrapper<UserProject>().eq(UserProject::getProjectId , appProject.getId()));
            userProjectService.saveBatch(userProjects);
        }
    }



    @Override
    public void delAppProject(Long projectId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, projectId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        AppProject bo = new AppProject();
        bo.setId(projectId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }

    @Override
    public List<AppProject> listAppProject() {
        return baseMapper.listAppProject(ssoService.isAdminToUserId(CurrentUserContext.getCurrentUser()));
    }
}
